package at.studentinformation;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.database.Cursor;

public class Schedule {
	public String SubjectId;
	public String StudentId;
	public String SemesterId;
	public int Day;
	public int Starthour;
	public int Hour;
	public String Room;
	public int Popular;
	public String Date;
	public String Week;
    
    public static Schedule ExamineFromJsonObject(JSONObject post)
	{
    	Schedule result = new Schedule();
		try
        {
            result.SubjectId = post.getString("SubjectId");
            result.StudentId = post.getString("StudentId");
            result.SemesterId = post.getString("SemesterId");
            result.Day = Integer.parseInt(post.getString("Day"));
            result.Starthour = Integer.parseInt(post.getString("Starthour"));
            result.Hour = Integer.parseInt(post.getString("Hour"));            
            result.Room = post.getString("Room");
            result.Popular = Integer.parseInt(post.getString("Popular"));
            result.Date = post.getString("Date");
            result.Week = post.getString("Week");
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;
	}
	
	public static ArrayList<Schedule> GetListSubjectFromJson(String input)
	{
		ArrayList<Schedule> result = new ArrayList<Schedule>();
		try
        {   			
            JSONArray entries = new JSONArray(input);
            for(int i =0; i<entries.length(); i++)
            {
            	JSONObject post = entries.getJSONObject(i);
            	Schedule temp = Schedule.ExamineFromJsonObject(post);
	            result.add(temp);
            }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;	
	}

	public static Schedule GetScheduleByCusor(Cursor c)
	{
		Schedule result = new Schedule();
		result.SubjectId = c.getString(c.getColumnIndex("SubjectId"));
		result.StudentId = c.getString(c.getColumnIndex("StudentId"));
		result.SemesterId = c.getString(c.getColumnIndex("SemesterId"));
		result.Day = Integer.parseInt(c.getString(c.getColumnIndex("Day")));
		result.Starthour = Integer.parseInt(c.getString(c.getColumnIndex("Starthour")));
		result.Hour = Integer.parseInt(c.getString(c.getColumnIndex("Hour")));
		result.Popular = Integer.parseInt(c.getString(c.getColumnIndex("Popular")));
		result.Room = c.getString(c.getColumnIndex("Room"));
		result.Date = c.getString(c.getColumnIndex("Date"));
		result.Week = c.getString(c.getColumnIndex("Week"));
		
		return result;
	}

	public static ArrayList<Schedule> GetListScheduleFormCursor(Cursor c)
	{
		ArrayList<Schedule> result = new ArrayList<Schedule>();
		while(c.moveToNext())
		{
			Schedule temp = Schedule.GetScheduleByCusor(c);
			result.add(temp);
		}
		return result;
	}
}
